package com.kcl.dfss;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class GuideActivity extends Activity{

	private ViewPager viewPager;
	private ArrayList<View> pageViews;
	private ImageView imageView;
	private ImageView[] imageViews;
	private Button btn_matching,btn_guide_login;
	
	private ViewGroup main;
	private LinearLayout group;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_guide);
		
		LayoutInflater inflater = getLayoutInflater();
		pageViews = new ArrayList<View>();
		pageViews.add(inflater.inflate(R.layout.guide_item01, null));
		pageViews.add(inflater.inflate(R.layout.guide_item02, null));
		pageViews.add(inflater.inflate(R.layout.guide_item03, null));
		
		imageViews = new ImageView[pageViews.size()];
		main = (ViewGroup) inflater.inflate(R.layout.activity_guide, null);
		group = (LinearLayout) main.findViewById(R.id.viewGroup);
		viewPager = (ViewPager) main.findViewById(R.id.guidePages);
		
		btn_matching = (Button) main.findViewById(R.id.btn_matching1);
		btn_guide_login = (Button) main.findViewById(R.id.btn_guide_login);
		btn_matching.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(GuideActivity.this,MatchingWiFiActivity.class);
				startActivity(intent);
			}
		});
		btn_guide_login.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(GuideActivity.this, LoginActivity.class);
				startActivity(intent);
			}
		});
		
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20,20);
		params.setMargins(25, 0, 25, 0);
		
		for (int i = 0; i < pageViews.size(); i++) {
			imageView = new ImageView(GuideActivity.this);
			imageView.setLayoutParams(params);
			imageViews[i] = imageView;
			
			if(i==0){
				imageViews[i].setBackgroundResource(R.drawable.point_true);
			}else{
				imageViews[i].setBackgroundResource(R.drawable.point_false);
			}
			group.addView(imageViews[i]);
		}
		setContentView(main);
		
		viewPager.setAdapter(new PagerAdapter() {

			@Override
			public int getCount() {
				return pageViews.size();
			}

			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == arg1;
			}

			@Override
			public void destroyItem(View container, int position, Object object) {
				((ViewPager)container).removeView(pageViews.get(position));
			}

			@Override
			public Object instantiateItem(View container, int position) {
				((ViewPager)container).addView(pageViews.get(position));
				return pageViews.get(position);
			}
			
		});
		
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				for(int i=0;i<imageViews.length;i++){
					imageViews[arg0].setBackgroundResource(R.drawable.point_true);
					if(arg0!=i){
						imageViews[i].setBackgroundResource(R.drawable.point_false);
					}
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});
		
		startLocationService();
	}
	
	private void startLocationService() {
		Intent intent = new Intent(this, LocationService.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startService(intent);
	}

}
